/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package me.zhengjie.modules.biz.rest;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.biz.domain.BarCode;
import me.zhengjie.modules.biz.service.BarCodeService;
import me.zhengjie.modules.biz.service.dto.BarCodeQueryCriteria;
import me.zhengjie.modules.biz.service.dto.HazardousChemicalsQueryCriteria;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Admin
 * @website https://eladmin.vip
 * @date 2023-03-03
 **/
@RestController
@RequiredArgsConstructor
@Api(tags = "bar-code管理")
@RequestMapping("/api/barCode")
public class BarCodeController {

    private final BarCodeService barCodeService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
//    @PreAuthorize("@el.check('barCode:list')")
    public void exportBarCode(HttpServletResponse response, BarCodeQueryCriteria criteria) throws IOException {
        barCodeService.download(barCodeService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询bar-code")
    @ApiOperation("查询bar-code")
//    @PreAuthorize("@el.check('barCode:list')")
    public ResponseEntity<Object> queryBarCode(BarCodeQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity<>(barCodeService.queryAll(criteria, pageable), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增bar-code")
    @ApiOperation("新增bar-code")
//    @PreAuthorize("@el.check('barCode:add')")
    public ResponseEntity<Object> createBarCode(@Validated @RequestBody BarCode resources) {
        return new ResponseEntity<>(barCodeService.create(resources), HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改bar-code")
    @ApiOperation("修改bar-code")
//    @PreAuthorize("@el.check('barCode:edit')")
    public ResponseEntity<Object> updateBarCode(@Validated @RequestBody BarCode resources) {
        barCodeService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除bar-code")
    @ApiOperation("删除bar-code")
//    @PreAuthorize("@el.check('barCode:del')")
    public ResponseEntity<Object> deleteBarCode(@RequestBody Integer[] ids) {
        barCodeService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @ApiOperation("打印")
    @AnonymousAccess
    @GetMapping(value = "/print")
    public ResponseEntity<Object> print(Integer id, Integer type, HttpServletResponse response) {
        barCodeService.printBarCode(id, type, response);
        return new ResponseEntity<>(HttpStatus.OK);
    }


    @AnonymousAccess
    @GetMapping(value = "/bulkPrint")
    @ApiOperation("批量打印")
    public ResponseEntity<Object> bulkPrint(String data, Integer type, HttpServletResponse response) {
        barCodeService.bulkPrint(data, type, response);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @AnonymousAccess
    @GetMapping(value = "/bulkPrintQRCode")
    @ApiOperation("批量打印")
    public ResponseEntity<Object> bulkPrintQRCode(String data, Integer type, HttpServletResponse response) {
        barCodeService.bulkPrintQRCode(data, type, response);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @AnonymousAccess
    @GetMapping(value = "/bulkPrintQRCodeSearchSundries")
    public ResponseEntity<Object> bulkPrintQRCodeSearchSundries(BarCodeQueryCriteria criteria, HttpServletResponse response) {
        barCodeService.bulkPrintQRCodeSearchSundries(criteria, response);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @AnonymousAccess
    @GetMapping(value = "/bulkPrintSearchSundries")
    public ResponseEntity<Object> bulkPrintSearchSundries(BarCodeQueryCriteria criteria, HttpServletResponse response) {
        barCodeService.bulkPrintSearchSundries(criteria, response);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @AnonymousAccess
    @GetMapping(value = "/bulkPrintQRCodeSearchHC")
    public ResponseEntity<Object> bulkPrintQRCodeSearchHC(HazardousChemicalsQueryCriteria criteria, HttpServletResponse response) {
        barCodeService.bulkPrintQRCodeSearchHC(criteria, response);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @AnonymousAccess
    @GetMapping(value = "/bulkPrintSearchHC")
    public ResponseEntity<Object> bulkPrintSearchHC(HazardousChemicalsQueryCriteria criteria, HttpServletResponse response) {
        barCodeService.bulkPrintSearchHC(criteria, response);
        return new ResponseEntity<>(HttpStatus.OK);
    }


    @ApiOperation("列表显示条形码")
    @AnonymousAccess
    @GetMapping("/showBarCode")
    public ResponseEntity<Object> show(Integer id, Integer type, Integer serialNumber, HttpServletResponse response) {
        barCodeService.showBarCode(id, type, serialNumber, response);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @ApiOperation("列表显示二维码")
    @AnonymousAccess
    @GetMapping("/showQRCode")
    public ResponseEntity<Object> showQRCode(Integer id, Integer type, Integer serialNumber, HttpServletResponse response) {
        barCodeService.showQRCode(id, type, serialNumber, response);
        return new ResponseEntity<>(HttpStatus.OK);
    }

//    @GetMapping("showQRCode")
//    @AnonymousAccess
//    public ResponseEntity<Object> showQRCode(Integer id,Integer type,Integer serialNumber,HttpServletResponse response){
//        try {
//            barCodeService.showQRCode(id,type,serialNumber,response);
//        }catch (Exception e){
////            response=new BaseResponse(StatusCode.Fail.getCode(),e.getMessage());
//        }
//        return new ResponseEntity<>(HttpStatus.OK);
//    }

    @ApiOperation("打印")
    @AnonymousAccess
    @GetMapping(value = "/printQRCode")
    public ResponseEntity<Object> printQRCode(Integer id, Integer type, HttpServletResponse response) {
        barCodeService.printQRCode(id, type, response);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
